var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");

var communityUrl = require("url/community");
var httpSilence = require("helper/httpSilence");
var qqHttp = require("helper/http");
var communityUtil = require("helper/community");
var helperOpen = require("helper/open");
var drawUrl = require("url/draw");

var rewardGrid = ui("rewardGrid");
var signDatas=[{"labelSign":"连续第1天","isSign":false,"bgSign":"EBEBEFFF","imgSign":"source://image/honor/gou.png"},
          {"labelSign":"连续第2天","isSign":false,"bgSign":"EBEBEFFF","imgSign":"source://image/honor/gou.png"},
          {"labelSign":"连续第3天","isSign":false,"bgSign":"EBEBEFFF","imgSign":"source://image/honor/gou.png"},
          {"labelSign":"连续第4天","isSign":false,"bgSign":"EBEBEFFF","imgSign":"source://image/honor/gou.png"},
          {"labelSign":"连续第5天","isSign":false,"bgSign":"EBEBEFFF","imgSign":"source://image/honor/gou.png"}];
var communityId;
do_Page.on("community_sign_tab_touch", function(index) {
	binddata();
	
});
var do_Button_1=ui("do_Button_1");
var addHaveView = ui("haveLayout").add("haveBorder","source://view/honor/index/module.ui");
//全部刷新数据
var signCommunity=1;
function binddata(){
	qqHttp.post(communityUrl.url.sign.detail,{},function(data){
		communityId=data.communityId;
		var times=data.times;
		var quanValue=0,honorValue=0;
		var quanValue2=0,honorValue2=0;
		var times2=data.times-1;
		for (var i = 0; i < data.list.length; i++) {
			var rule=data.list[i];
			if(rule.compare==2){
				if(times==rule.compareValue){
					quanValue=rule.quanValue;
					honorValue=rule.honorValue;
				}
				if(times2==rule.compareValue){
					quanValue2=rule.quanValue;
					honorValue2=rule.honorValue;
				}
				rule.title={"centerView":{"title01":"连续签到","titlenum":rule.compareValue,"title02":"天"}};
			}else if(rule.compare==1){
				rule.title={"centerView":{"title01":"连续签到大于","titlenum":rule.compareValue,"title02":"天"}};
				if(times>rule.compareValue){
					quanValue=rule.quanValue;
					honorValue=rule.honorValue;
				}
				if(times2>rule.compareValue){
					quanValue2=rule.quanValue;
					honorValue2=rule.honorValue;
				}
			}
			
		}
		signCommunity=data.signCommunity;
		if(data.signCommunity==2){
			do_Button_1.text="签到";
			do_Button_1.bgColor="FF5305FF";
			ui("do_Label_21").text="第"+times+"天获得";
			ui("do_Label_6").text=honorValue;
			ui("do_Label_8").text=quanValue;
		}else{
			do_Button_1.text="已签到";
			do_Button_1.bgColor="949EAAFF";
			ui("do_Label_21").text="第"+times2+"天获得";
			ui("do_Label_6").text=honorValue2;
			ui("do_Label_8").text=quanValue2;
		}
		ui(addHaveView + ".haveView").items={"centerView":{"label01":"已累计获得：","label02":data.totalHonor+"荣誉值+"+data.totalQuan+"圈币"}};
		ui("signGrid").items=signDatas;
		rewardGrid.items=data.list;
		for (var i = 0; i < signDatas.length; i++) {
			if(times-1>i){
				signDatas[i].isSign=true;
				signDatas[i].bgSign="ff5305FF";
				signDatas[i].imgSign="source://image/honor/gou2.png";
			}else{
				signDatas[i].isSign=false;
				signDatas[i].bgSign="EBEBEFFF";
				signDatas[i].imgSign="source://image/honor/gou.png";
			}
		}
		ui("signGrid").items=signDatas;
		ui("do_Button_1").visible=true;
		ui("do_ScrollView_1").toBegin();
	});
}
do_Button_1.on("touch", "", 300,function(data) {
	if(signCommunity==2){
		communitySign();
	}
});
ui("do_ALayout_6").on("touch",function(data) {
	helperOpen.open("source://view/honor/result/list.ui");
});
ui("do_ALayout_7").on("touch",function(data) {
	helperOpen.open("source://view/user/money/quanList.ui");
});
//签到社区
function communitySign() {
	var params = {
		communityId : communityUtil.communityVisitId()
	};
	httpSilence.post(communityUrl.url.sign.add, params, function(data) {
		if(data.isSign==1){//签到成功
			do_Page.fire("community_sign_back", {signCommunity:1});
			binddata();
			deviceone.print(data.communityBaseVo.isLottery==1);
			if(data.communityBaseVo.isLottery==1){
				loadLottery();
			}
		}
	})
}
function loadLottery(){
	qqHttp.post(drawUrl.url.base.detail, {communityId:communityId,type:1}, function(data) {
		if(null!=data.drawBaseVo){
			var drawId=data.drawBaseVo.id;
			var rewardNames=[];
			var imgs=[];
			for (var i = 0; i < data.items.length; i++) {
				var item=data.items[i];
				rewardNames.push(item.drawItemName);
				imgs.push(item.drawItemImg);
				
			}
			helperOpen.open("source://view/lottery/index.ui",{userDrawList:data.userDrawList,type:1,bizzId:communityId,drawId:drawId,rewardNames:rewardNames,imgs:imgs});
		}
	});
}